/*
 * This header, excluding the #ifdef __KERNEL__ part, is BSD licensed so
 * anyone can use the definitions to implement compatible drivers/servers:
 *
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of IBM nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifndef _LINUX_VIRTIO_QTBOX_H
#define _LINUX_VIRTIO_QTBOX_H

#include <linux/types.h>
#include <linux/virtio_types.h>
#include <linux/virtio_ids.h>
#include <linux/virtio_config.h>
#include "virtio_qtbox_errno.h"

#define VIRTIO_QTBOX_F_SANDBOX_VM	0

struct virtio_qtbox_config {
	__le32 max_dataqueues;
#define VIRTIO_QTBOX_CONFIG_RSVD_NR	4
	__le32 rsvd[VIRTIO_QTBOX_CONFIG_RSVD_NR];
};

#define VIRTIO_QTBOX_SANDBOX_VM_SERVICE		1
  #define VIRTIO_QTBOX_SANDBOX_VM_S_INSTANCE_CAP		0
  #define VIRTIO_QTBOX_SANDBOX_VM_S_INSTANCE_DEFINE		1
  #define VIRTIO_QTBOX_SANDBOX_VM_S_INSTANCE_ATTACH_RESOURCE	2
  #define VIRTIO_QTBOX_SANDBOX_VM_S_INSTANCE_START		3
  #define VIRTIO_QTBOX_SANDBOX_VM_S_INSTANCE_SHUTDOWN		4
  #define VIRTIO_QTBOX_SANDBOX_VM_S_INSTANCE_INFO		5
  #define VIRTIO_QTBOX_SANDBOX_VM_S_INSTANCE_UNDEFINE		6

struct virtio_qtbox_cmd_header {
	__le32 op_code;
	__le32 subcode;
	__le32 rsvd1;
	__le32 rsvd2;
};


/**
 * Sandbox Service: Common Structure Of Result.
 */

struct virtio_qtbox_sandbox_vm_cmd_result_comm {
	__le32 rv;
	__le32 rsvd;
	__le64 sid; /* Sandbox ID */
};


/**
 * Sandbox Service: Get Capability Command.
 */

struct virtio_qtbox_sandbox_vm_instance_cap_flf {
	__le64 rsvd;
};

struct virtio_qtbox_sandbox_vm_instance_cap_result {
	__le64 max_instances;
	__le64 max_vcpus_num;
	__le64 max_mem_regions_num;
	__le64 max_pdevs_num;
	__le64 mem_addr_align;
	__le64 mem_size_align;
#define VIRTIO_QTBOX_SANDBOX_VM_QUIET_MODE	(1 << 0)
#define VIRTIO_QTBOX_SANDBOX_VM_DEBUG_MODE	(1 << 1)
	__le64 supported_launch_mode;
#define VIRTIO_QTBOX_SANDBOX_VM_EIF_FORMAT	(1 << 0)
	__le64 supported_image_format;
	__le64 image_offset;
};


/**
 * Sandbox Service: Define Instance Command.
 */

struct virtio_qtbox_sandbox_vm_instance_define_flf {
	__le64 launch_mode;
	__le64 image_format;
};


/**
 * Sandbox Service: Add Resources Command.
 */

struct virtio_qtbox_sandbox_vm_instance_attach_resources_flf {
	__le32 sid; /* Sandbox ID */
	__le32 num_vcpus;
	__le32 num_mems_region;
	__le32 num_pdevs;
	__le32 rsvd1;
	__le32 rsvd2;
};

struct virtio_qtbox_sandbox_vm_add_vcpu {
	__le32 vcpu_id;
	__le32 rsvd;
};

struct virtio_qtbox_sandbox_vm_add_mem {
	__le64 paddr;
	__le64 size;
	__le64 rsvd;
};

struct virtio_qtbox_sandbox_vm_add_pdev {
	__le16 domain;
	__u8  bus;
	__u8  devfn;
	__le32 rsvd;
};


/**
 * Sandbox Service: Start Instance Command.
 */

struct virtio_qtbox_sandbox_vm_instance_start_flf {
	__le64 sid; /* Sandbox ID */
	__le64 cid; /* vsock cid */
	__le64 rsvd;
};


/**
 * Sandbox Service: Stop Instance Command.
 */

struct virtio_qtbox_sandbox_vm_instance_stop_flf {
	__le64 sid; /* Sandbox ID */
};


/**
 * Sandbox Service: Stop Instance Command.
 */

struct virtio_qtbox_sandbox_vm_instance_info_flf {
	__le64 sid; /* Sandbox ID */
};

struct virtio_qtbox_sandbox_vm_instance_info_result {
	struct virtio_qtbox_sandbox_vm_cmd_result_comm comm;
	__le64 cid;
	__le64 vcpus;
	__le64 mem_regions;
	__le64 pdevs;
	__le64 flags;
#define VIRTIO_QTBOX_SANDBOX_VM_STATE_INITIAL	0
#define VIRTIO_QTBOX_SANDBOX_VM_STATE_DEFINED	1
#define VIRTIO_QTBOX_SANDBOX_VM_STATE_RUNNING	2
#define VIRTIO_QTBOX_SANDBOX_VM_STATE_STOPPED	3
	__le64 state;
#define VIRTIO_QTBOX_SANDBOX_VM_INFO_RSVD_NR	8
	__le64 rsvd[VIRTIO_QTBOX_SANDBOX_VM_INFO_RSVD_NR];
};

/**
 * Sandbox Service: Undefine Instance Command.
 */

struct virtio_qtbox_sandbox_vm_instance_undefine_flf {
	__le64 sid; /* Sandbox ID */
};

struct virtio_qtbox_event_elem {
#define VIRTIO_QTBOX_SANDBOX_VM_E_CHECK_STATE	0
	__le32 op_code;
	__le32 event;

	/* op_code specific */
#define VIRTIO_QTBOX_SANDBOX_VM_E_EXTRA_PARA_LEN 32
	__u8 extra_para[VIRTIO_QTBOX_SANDBOX_VM_E_EXTRA_PARA_LEN];
};

struct log_msg {
	__le64 sid;
	__le32 len;
	__le16 type;

#define DEFAULT_DATA_QUEUE_SHIFT  13
#define DEFAULT_DATA_QUEUE_SIZE (1 << DEFAULT_DATA_QUEUE_SHIFT)
	__u8 buf[DEFAULT_DATA_QUEUE_SIZE];
};
#endif
